extends Node
class_name SpiritRoot

# 灵根类型
enum RootType {
	# 基础灵根
	GOLD,    # 金灵根
	WOOD,    # 木灵根
	WATER,   # 水灵根
	FIRE,    # 火灵根
	EARTH,   # 土灵根
	# 稀有灵根 (5%概率)
	ICE,     # 冰灵根
	THUNDER, # 雷灵根
	WIND,    # 风灵根
	LIGHT,   # 光灵根
	DARK,    # 暗灵根
	# 变异灵根 (1%概率)
	CHAOS,   # 混沌灵根
	HEAVEN_DEFYING # 逆天灵根
}

# 灵根配置
const ROOT_CONFIG = {
	# 基础灵根
	RootType.GOLD: {
		"name": "金灵根",
		"desc": "灵气增长 +10%",
		"emoji": "⚱️",
		"qi_bonus": 0.10,
		"speed_bonus": 0.0,
		"success_rate_bonus": 0.0,
		"rarity": "basic",
		"color": Color(1.0, 0.85, 0.2)  # 明亮金色
	},
	RootType.WOOD: {
		"name": "木灵根",
		"desc": "修炼速度 +10%",
		"emoji": "🌿",
		"qi_bonus": 0.0,
		"speed_bonus": 0.10,
		"success_rate_bonus": 0.0,
		"rarity": "basic",
		"color": Color(0.3, 0.85, 0.3)  # 翠绿色
	},
	RootType.WATER: {
		"name": "水灵根",
		"desc": "突破成功率 +10%",
		"emoji": "💧",
		"qi_bonus": 0.0,
		"speed_bonus": 0.0,
		"success_rate_bonus": 0.10,
		"rarity": "basic",
		"color": Color(0.2, 0.8, 1.0)  # 清澈蓝色
	},
	RootType.FIRE: {
		"name": "火灵根",
		"desc": "灵气增长 +15%",
		"emoji": "🔥",
		"qi_bonus": 0.15,
		"speed_bonus": 0.0,
		"success_rate_bonus": 0.0,
		"rarity": "basic",
		"color": Color(1.0, 0.4, 0.1)  # 火焰橙红
	},
	RootType.EARTH: {
		"name": "土灵根",
		"desc": "突破成功率 +5%, 减少失败损失",
		"emoji": "🏔️",
		"qi_bonus": 0.0,
		"speed_bonus": 0.0,
		"success_rate_bonus": 0.05,
		"rarity": "basic",
		"color": Color(0.8, 0.6, 0.3)  # 大地褐色
	},
	# 稀有灵根
	RootType.ICE: {
		"name": "冰灵根",
		"desc": "灵气获取 +20%, 突破成功率 +5%",
		"emoji": "❄️",
		"qi_bonus": 0.20,
		"speed_bonus": 0.0,
		"success_rate_bonus": 0.05,
		"rarity": "rare",
		"color": Color(0.5, 0.9, 1.0)  # 冰晶蓝
	},
	RootType.THUNDER: {
		"name": "雷灵根",
		"desc": "灵气增长 +50%",
		"emoji": "⚡",
		"qi_bonus": 0.50,
		"speed_bonus": 0.0,
		"success_rate_bonus": 0.0,
		"rarity": "rare",
		"color": Color(0.7, 0.3, 1.0)  # 雷电紫
	},
	RootType.WIND: {
		"name": "风灵根",
		"desc": "修炼速度 +25%",
		"emoji": "💨",
		"qi_bonus": 0.0,
		"speed_bonus": 0.25,
		"success_rate_bonus": 0.0,
		"rarity": "rare",
		"color": Color(0.4, 1.0, 0.7)  # 清风青
	},
	RootType.LIGHT: {
		"name": "光灵根",
		"desc": "灵气转化效率 +30%",
		"emoji": "✨",
		"qi_bonus": 0.30,
		"speed_bonus": 0.0,
		"success_rate_bonus": 0.0,
		"rarity": "rare",
		"color": Color(1.0, 1.0, 0.6)  # 圣光金
	},
	RootType.DARK: {
		"name": "暗灵根",
		"desc": "灵气增长 +20%",
		"emoji": "🌑",
		"qi_bonus": 0.20,
		"speed_bonus": 0.0,
		"success_rate_bonus": 0.0,
		"rarity": "rare",
		"color": Color(0.5, 0.4, 0.6)  # 幽暗紫
	},
	# 变异灵根
	RootType.CHAOS: {
		"name": "混沌灵根",
		"desc": "修炼速度 +100% (极其稀有)",
		"emoji": "🌀",
		"qi_bonus": 0.0,
		"speed_bonus": 1.0,
		"success_rate_bonus": 0.0,
		"rarity": "legendary",
		"color": Color(0.8, 0.2, 0.9)  # 混沌紫
	},
	RootType.HEAVEN_DEFYING: {
		"name": "逆天灵根",
		"desc": "灵气增长 +300% (万中无一)",
		"emoji": "🌟",
		"qi_bonus": 3.0,
		"speed_bonus": 0.0,
		"success_rate_bonus": 0.0,
		"rarity": "legendary",
		"color": Color(1.0, 0.3, 0.6)  # 逆天粉
	}
}

# 当前拥有的灵根
var roots: Array[RootType] = []

# 生成随机灵根
func generate_random_roots() -> Array[RootType]:
	roots.clear()
	
	# 1. 生成1-3个基础灵根
	var basic_roots = [RootType.GOLD, RootType.WOOD, RootType.WATER, RootType.FIRE, RootType.EARTH]
	var num_basic = randi_range(1, 3)
	basic_roots.shuffle()
	
	for i in range(num_basic):
		roots.append(basic_roots[i])
	
	# 2. 5%概率额外获得稀有灵根
	if randf() < 0.05:
		var rare_roots = [RootType.ICE, RootType.THUNDER, RootType.WIND, RootType.LIGHT, RootType.DARK]
		roots.append(rare_roots[randi() % rare_roots.size()])
	
	# 3. 1%概率额外获得变异灵根
	if randf() < 0.01:
		var legendary_roots = [RootType.CHAOS, RootType.HEAVEN_DEFYING]
		roots.append(legendary_roots[randi() % legendary_roots.size()])
	
	return roots

# 计算总加成
func get_total_qi_bonus() -> float:
	var total = 0.0
	for root in roots:
		total += ROOT_CONFIG[root]["qi_bonus"]
	return total

func get_total_speed_bonus() -> float:
	var total = 0.0
	for root in roots:
		total += ROOT_CONFIG[root]["speed_bonus"]
	return total

func get_total_success_rate_bonus() -> float:
	var total = 0.0
	for root in roots:
		total += ROOT_CONFIG[root]["success_rate_bonus"]
	return total

# 获取灵根信息
func get_root_info(root_type: RootType) -> Dictionary:
	return ROOT_CONFIG[root_type]

# 检查是否有土灵根(减少失败损失)
func has_earth_root() -> bool:
	return RootType.EARTH in roots

# 获取所有灵根描述
func get_all_roots_description() -> String:
	var desc = ""
	for root in roots:
		var info = ROOT_CONFIG[root]
		desc += info["name"] + ": " + info["desc"] + "\n"
	return desc
